--[[-------------------------------------------------------------------------
  Copyright (c) 2007, Trond A Ekseth
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials provided
        with the distribution.
      * Neither the name of oMirrorBars nor the names of its contributors
        may be used to endorse or promote products derived from this
        software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------]]
-- oMirrorBars - core/bars.lua

local G = getfenv(0)
local name = "oMirrorBars"

local core = G[name]

local class = CreateFrame"Frame"
local mt = {__index = class}

local colors = MirrorTimerColors
local GetMirrorTimerInfo = GetMirrorTimerInfo

local OnUpdate = function(self, elapsed)
	if(self.paused) then return end

	self.bar:SetValue(GetMirrorTimerProgress(self.type))
end

local onDragStop = function(self)
	self:StopMovingOrSizing()
	core:savePosition(self.type)
end
local onDragStart = function(self)
	self:StartMoving()
end

-- _PAUSE doesn't have a type when it fires, if it does fire at all.
local OnEvent = function(self, event, ...)
	self:pause(...)
end

local backdrops = {
	['shade'] = {
		bgFile = "Interface\\ChatFrame\\ChatFrameBackground", tile = true, tileSize = 16,
		insets = {left = 6, right = 2, top = 6, bottom = 2},
	},
}
local spawnFrame = function(type)
	local frame = CreateFrame("Frame", name..type.."Frame", UIParent)
	setmetatable(frame, mt)
	core.frames[type] = frame
	frame.type = type

	frame:RegisterEvent"MIRROR_TIMER_PAUSE"
	frame:SetScript("OnEvent", OnEvent)

	frame:SetScript("OnDragStart", onDragStart)
	frame:SetScript("OnDragStop", onDragStop)

	frame:SetMovable(true)
	frame:EnableMouse(true)
	frame:RegisterForDrag"LeftButton"

	-- We set it per frame, as it's more efficient then a shared handler. A
	-- shared handler will use about 65% more CPU. Based on test from the
	-- 2.1.0 PTR.
	frame:SetScript("OnUpdate", OnUpdate)

	local tex = frame:CreateTexture(nil, "OVERLAY")
	frame.tex = tex

	tex:SetPoint("LEFT", frame, 4, 0)
	tex:SetPoint("RIGHT", frame, -4, 0)
	tex:SetPoint("TOP", frame, 0, -4)
	tex:SetPoint("BOTTOM", frame, 0, 4)

	local bar = CreateFrame("StatusBar", nil, frame)
	frame.bar = bar

	bar:SetPoint("LEFT", frame, 4, 0)
	bar:SetPoint("RIGHT", frame, -4, 0)
	bar:SetPoint("TOP", frame, 0, -4)
	bar:SetPoint("BOTTOM", frame, 0, 4)

	local text = bar:CreateFontString(nil, "OVERLAY")
	frame.text = text

	text:SetJustifyH"CENTER"
	text:SetShadowOffset(.8, -.8)
	text:SetShadowColor(0, 0, 0, 1)

	text:SetPoint("LEFT", bar)
	text:SetPoint("RIGHT", bar)

	frame:setStyle()

	return frame
end

function class:new(type)
	local frame = spawnFrame(type)
	core.frames[type] = frame

	local p1, parent, p2, x, y = core:loadPosition(type)
	frame:SetPoint(p1, parent, p2, x, y)

	return frame
end

function class:setStyle()
	local type = self.type
	local db = core.profile

	local tex = self.tex
	local bar = self.bar
	local text = self.text

	self:SetWidth(db.width)
	self:SetHeight(db.height)
	self:SetBackdrop(backdrops[db.backdrop])
	self:SetBackdropColor(0, 0, 0, .4)

	local r, g, b = colors[type].r, colors[type].g, colors[type].b
	tex:SetTexture"Interface\\AddOns\\oMirrorBars\\textures\\glaze"
	tex:SetVertexColor(r*.5, g*.5, b*.5)

	bar:SetStatusBarTexture"Interface\\AddOns\\oMirrorBars\\textures\\glaze"
	bar:SetStatusBarColor(r, g, b)

	text:SetFont(STANDARD_TEXT_FONT, db.textSize)
end

function class:start(value, maxValue, scale, pause, text)
	self.scale = scale

	if(pause > 0) then self.pause = 1
	elseif(self.pause) then self.pause = nil end

	self.text:SetText(text)

	self.bar:SetMinMaxValues(0, maxValue)
	self.bar:SetValue(value)

	if(not self:IsShown()) then self:Show() end
end

function class:stop()
	self:Hide()
end

function class:pause(status)
	if(status > 0) then
		self.paused = 1
	else
		self.paused = nil
	end
end

core.bars = class
